package com.gok.day1.web;

import com.gok.day1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/")
public class IndexController {
    public final static List<User> users = new ArrayList<>();
    @GetMapping("login")
    public String login(){
        return "login";
    }
    @GetMapping("regist")
    public String regist(){
        return "regist";
    }
    @PostMapping("tologin")
    public String tologin(User user){
        if(user==null||users.stream().filter(u -> u.getName().equals(user.getName())).count()==0){
            return "regist";
        }
        List<User> collect = users.stream().filter(u -> u.getName().equals(user.getName()) && u.getPassword().equals(user.getPassword())).collect(Collectors.toList());
        if(collect.size()!=0){
            return "index";
        }
        return "login";
    }
    @PostMapping("toRegist")
    public String regist(User user){
        List<User> collect = users.stream().filter(u -> u.getName().equals(user.getName()) && u.getPassword().equals(user.getPassword())).collect(Collectors.toList());
        if(user!=null&&user.getName()!=null&&user.getPassword()!=null){

            if(collect.size()==0){
                users.add(user);
            }else {
                return "failRegist";
            }

            return "login";
        }
        return "regist";
    }

}
